package tasks.leetcode;

public class TaskMaxPrice {
    public static void main(String[] args) {

    }

    /**
     * 一次循环；关键点在于，获取最低的价值，然后从后面的元素中，获取最大的差值
     *
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        int maxValues = 0;
        int minPrice = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            } else if (prices[i] - minPrice > maxValues) {
                maxValues = Math.max(prices[i] - minPrice, maxValues);
            }
        }
        return maxValues;
    }
}
